﻿#include "QWinMainForm.h"

QWinMainForm::QWinMainForm(QWidget *parent) : QWidget(parent)
{
    setObjectName(tr("baseForm"));
    IsTitleMousePress = false;

    setMinimumHeight(240);
    setMinimumWidth(320);

    resize(1024, 720);

    setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint |
                   Qt::WindowMinimizeButtonHint | windowFlags());

    PositionCalculator = new QPositionCalculator(this);

    MainFormTitleBar = new QMainFormTitleBar(this);
    MainFormTitleBar->setObjectName(tr("mainFormTitleBar"));
    MainFormTitleBar->move(1, 1);

    installEventFilter(this);
    connect(MainFormTitleBar, SIGNAL(TitleMouseDoubleClickEvent(QMouseEvent*)),
            this, SLOT(mouseDoubleClickEvent(QMouseEvent*)));

    connect(MainFormTitleBar, SIGNAL(TitleMousePressEvent(QMouseEvent*)),
            this, SLOT(mousePressEvent(QMouseEvent*)));
    connect(MainFormTitleBar, SIGNAL(TitleMouseReleaseEvent(QMouseEvent*)),
            this, SLOT(mouseReleaseEvent(QMouseEvent*)));
    connect(MainFormTitleBar, SIGNAL(TitleMouseMoveEvent(QMouseEvent*)),
            this, SLOT(mouseMoveEvent(QMouseEvent*)));

    connect(MainFormTitleBar, SIGNAL(ShowMinimized()), this, SLOT(showMinimized()));
    connect(MainFormTitleBar, SIGNAL(ShowMaximized()), this, SLOT(showMaximizedMinimized()));
    connect(MainFormTitleBar, SIGNAL(CloseForm()), this, SLOT(close()));

    StyleSheetFile = new QFile(tr(":/resource/qss/silvery.css"));

    if(!StyleSheetFile->open(QFile::ReadOnly)) {
        qDebug() << "open failed";
    }
    QString StyleSheetText = StyleSheetFile->readAll();
    setStyleSheet(StyleSheetText);
    setWindowTitle(tr("Custom Style Sheet"));
    WindowsCentering();
    connect(this, SIGNAL(MouseMoveMainForm(QPoint)), this, SLOT(moveMainForm(QPoint)));
}

QWinMainForm::~QWinMainForm()
{
    delete StyleSheetFile;
}

void QWinMainForm::resizeEvent(QResizeEvent *event)
{
    if(event != NULL) {

    }
    int MainFormWidth = width();
    int MainFormHeight = height();

    if(!isMaximized()) {
        FormNormalSize = size();
    }

    MainFormTitleBar->resize(MainFormWidth-2, 30);
}

void QWinMainForm::paintEvent(QPaintEvent* paintEvent)
{
    if(paintEvent != NULL) {

    }

    QStyleOption StyleOption;
    StyleOption.init(this);
    QPainter Painter(this);
    style()->drawPrimitive(QStyle::PE_Widget, &StyleOption, &Painter, this);
}

void QWinMainForm::mouseDoubleClickEvent(QMouseEvent *event)
{
    QMainFormTitleBar* TitleBar = qobject_cast<QMainFormTitleBar*>(sender());
    if(TitleBar == MainFormTitleBar) {
        if(event->button() == Qt::LeftButton) {
            if(!isMaximized()) {
                showMaximized();
                MainFormTitleBar->SetMaximizeButtonIcon(QChar(0xf079));
            } else if(isMaximized()) {
                showNormal();
                MainFormTitleBar->SetMaximizeButtonIcon(QChar(0xf096));
            }
        }
    }
}

void QWinMainForm::mouseMoveEvent(QMouseEvent *event)
{
    static int count = 0;
    QMainFormTitleBar* TitleBar = qobject_cast<QMainFormTitleBar*>(sender());
    if(TitleBar == MainFormTitleBar && IsTitleMousePress) {
        if(isMaximized()) {
            showNormal();
            MainFormTitleBar->SetMaximizeButtonIcon(QChar(0xf096));
            /** 计算鼠标所在窗口标题栏的位置比例 */
            double MouseXPosition = event->pos().x();
            double FormWidth = width();
            double FormHeight = height();
            double xProportionPositoin = MouseXPosition / FormWidth;
            double yProportionPositoin = MouseXPosition / FormHeight;
            resize(FormNormalSize);
            int xPosition = FormNormalSize.width() * xProportionPositoin;
            int yPosition = FormNormalSize.height() * yProportionPositoin;
            xPosition = xPosition > (FormNormalSize.width() - 120) ?
                        (FormNormalSize.width() - 145) : xPosition;
            yPosition = yPosition < 0 || yPosition > 30 ? 15 : xPosition;
            move(event->pos().x() - xPosition, event->globalPos().y() - yPosition);
            MousePosition = event->globalPos() - pos();
        }
        QDesktopWidget Desktop;

        if(!isMaximized() &&
                (event->globalPos().y() < Desktop.availableGeometry().height())) {
            move(event->globalPos() - MousePosition);
        }
    }
    emit MouseMoveMainForm(event->globalPos());
    ++ count;
}

void QWinMainForm::mousePressEvent(QMouseEvent *event)
{
    QMainFormTitleBar* TitleBar = qobject_cast<QMainFormTitleBar*>(sender());
    if(TitleBar == MainFormTitleBar && event->button() == Qt::LeftButton) {
        MousePosition = event->globalPos() - pos();
        IsTitleMousePress = true;
    }
}

void QWinMainForm::mouseReleaseEvent(QMouseEvent *event)
{
    QMainFormTitleBar* TitleBar = qobject_cast<QMainFormTitleBar*>(sender());
    if(TitleBar == MainFormTitleBar && event->button() == Qt::LeftButton) {
        qDebug() << "mouseReleaseEvent";
        IsTitleMousePress = false;
    }
}

void QWinMainForm::leaveEvent()
{

}

void QWinMainForm::hoverMoveEvent(QMouseEvent *event)
{
    qDebug() << "hoverMoveEvent";
}

bool QWinMainForm::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    return QWidget::nativeEvent(eventType, message, result);
}

bool QWinMainForm::eventFilter(QObject *obj, QEvent *event)
{
    QEvent::Type EventIdentifier = event->type();
    switch (EventIdentifier) {
        case QEvent::WindowTitleChange: {
                MainFormTitleBar->SetTitleText(windowTitle());
                return true;
            }
            break;

        case QEvent::WindowIconChange: {
                MainFormTitleBar->SetTitleIcon(QChar(0xf015));
                return true;
            }

        case QEvent::WindowStateChange: {
                return QWidget::eventFilter(obj, event);
            }
            break;

        case QEvent::Resize: {
                return QWidget::eventFilter(obj, event);
            }
            break;

        case QEvent::MouseButtonDblClick: {
                return QWidget::eventFilter(obj, event);
            }
            break;

        case QEvent::MouseButtonPress: {
                return QWidget::eventFilter(obj, event);
            }
            break;

        case QEvent::MouseButtonRelease: {
                return QWidget::eventFilter(obj, event);
            }
            break;

        case QEvent::MouseMove: {
                return QWidget::eventFilter(obj, event);
            }
            break;

        case QEvent::Leave: {
                //qDebug() << "Leave";
                return QWidget::eventFilter(obj, event);
            }
            break;

        case QEvent::HoverMove: {
                //qDebug() << "HoverMove";
                QMouseEvent* MouseEvent = static_cast<QMouseEvent*>(event);
                PositionCalculator->UpdateCursorShape(mapToGlobal(MouseEvent->pos()));
                return QWidget::eventFilter(obj, event);
            }
            break;

        default: {
                return QWidget::eventFilter(obj, event);
            }
            break;
    }


    return QWidget::eventFilter(obj, event);
}

void QWinMainForm::showMaximizedMinimized()
{
    if(!isMaximized()) {
        showMaximized();
        MainFormTitleBar->SetMaximizeButtonIcon(QChar(0xf079));
    } else if(isMaximized()) {
        showNormal();
        MainFormTitleBar->SetMaximizeButtonIcon(QChar(0xf096));
    }
}

void QWinMainForm::moveMainForm(QPoint Position)
{
    int x = Position.x();
    int y = Position.y();
    int wX = pos().x();
    int wY = pos().y();
    if((x - wX) >= 0 && (y - wY) >= 0) {
        //move(0, 0);
    }
}
